﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lib.Data;
using Lib.Utils;

namespace SysObj
{
    public class Bg_KhuVuc_GiaLe
    {
        public void Insert(DataLayer db, DataTable Data, int IdGiaKhuVuc)
        {
            if (Data == null || Data.Rows.Count <= 0) return;

            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                foreach (DataRow row in Data.Rows)
                {
                    ParamHolder.ClearAll();
                    ParamHolder.AddParam("Command", "Insert");
                    ParamHolder.AddParam("IdGiaKhuVuc", IdGiaKhuVuc);

                    if (Data.Columns.Contains("IdDoiTuong"))
                        ParamHolder.AddParam("IdDoiTuong", Cvert.ToInt32(row["IdDoiTuong"], 0));
                    else if (Data.Columns.Contains("IdHangHoa"))
                        ParamHolder.AddParam("IdDoiTuong", Cvert.ToInt32(row["IdHangHoa"], 0));
                    else if (Data.Columns.Contains("IdNhom"))
                        ParamHolder.AddParam("IdDoiTuong", Cvert.ToInt32(row["IdNhom"], 0));

                    ParamHolder.AddParam("PhanTram", Cvert.ToInt32(row["PhanTram"], 0));
                    ParamHolder.AddParam("SoTien", Cvert.ToDecimal(row["SoTien"], 0));
                    ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                    db.ExecNonQuery("Sp_Bg_KhuVuc_GiaLe", s_ParamName, o_ParamValue);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void OnDelete(DataLayer db, int IdGiaKhuVuc)
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                ParamHolder.ClearAll();
                ParamHolder.AddParam("Command", "Delete");
                ParamHolder.AddParam("IdGiaKhuVuc", IdGiaKhuVuc);
                ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                db.ExecNonQuery("Sp_Bg_KhuVuc_GiaLe", s_ParamName, o_ParamValue);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LoadByGroup(DataLayer db, int IdGiaKhuVuc)
        {
            return db.ExecDataTable("Sp_Bg_KhuVuc_GiaLe", "Command,IdGiaKhuVuc", "LoadByGroup", IdGiaKhuVuc);
        }

        public DataTable LoadByMenu(DataLayer db, int IdGiaKhuVuc)
        {
            return db.ExecDataTable("Sp_Bg_KhuVuc_GiaLe", "Command,IdGiaKhuVuc", "LoadByMenu", IdGiaKhuVuc);
        }
    }
}
